
// 17.20

#include "802_1w_2001_procedures.h"
#include "bridge.h"
#include "port.h"
#include "assert.h"

enum
{
	UNDEFINED,
	ONE_SECOND,
	TICK,
};

// ============================================================================

const char*	PortTimers_802_1w_2001_GetStateName (SM_STATE state)
{
	switch (state)
	{
		case ONE_SECOND:	return "ONE_SECOND";
		case TICK:			return "TICK";
		default:			return "(undefined)";
	}
}

// ============================================================================

SM_STATE PortTimers_802_1w_2001_CheckConditions (BRIDGE* bridge, int givenPort, int givenTree, SM_STATE state)
{
	assert (givenPort != -1);
	assert (givenTree == -1);

	PORT* port = bridge->ports [givenPort];
	
	// ------------------------------------------------------------------------
	// Check global conditions.
	
	if (bridge->BEGIN)
	{
		if (state == ONE_SECOND)
		{
			// The entry block for this state has been executed already.
			return 0;
		}
		
		return ONE_SECOND;
	}
	
	// ------------------------------------------------------------------------
	// Check exit conditions from each state.

	if (state == ONE_SECOND)
	{
		if (port->tick)
			return TICK;
		
		return 0;
	}

	if (state == TICK)
		return ONE_SECOND;
	
	assert (false);
	return 0;
}

// ============================================================================

void PortTimers_802_1w_2001_InitState (BRIDGE* bridge, int givenPort, int givenTree, SM_STATE state)
{
	assert (givenPort != -1);
	assert (givenTree == -1);
	
	PORT* port = bridge->ports [givenPort];
	PORT_TREE* portTree = port->trees [CIST_INDEX];
	
	if (state == ONE_SECOND)
	{
		port->tick = false;
	}
	else if (state == TICK)
	{
		if (port->helloWhen         > 0) port->helloWhen--;
		if (portTree->tcWhile       > 0) portTree->tcWhile--;
		if (portTree->fdWhile       > 0) portTree->fdWhile--;
		if (portTree->rcvdInfoWhile > 0) portTree->rcvdInfoWhile--;
		if (portTree->rrWhile 		> 0) portTree->rrWhile--;
		if (portTree->rbWhile		> 0) portTree->rbWhile--;
		if (port->mDelayWhile		> 0) port->mDelayWhile--;
		if (port->txCount			> 0) port->txCount--;
	}
	else
		assert (false);
}


// ============================================================================
